package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
/*
 * 其他可以简写，比如ShoppingCartService，ShoppingCartMapper，ShoppingCartController，但是
 * @Return业务层必须注释写详细，为了能更快理解业务，分析业务，代码开发，逻辑更加清晰
 */
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    public void add(ShoppingCartDTO shoppingCartDTO) {  //ShoppingCartDTO: dishId setmealId dishFlavor
        //1.判断当前加入到购物车的数据(商品或套餐)是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        //将dto数据复制到对象中,同时设置用户id
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId((Long) redisTemplate.opsForValue().get("user_4"));
        //list集合只有一条数据即get(0)，因为加入到购物车的数据（若dishId或setmealId和数据库中记录相同，且对应的userId相同，数据库中只能存储一份）
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            //2.1 如果在购物车,更新number
            shoppingCartMapper.updateNumberByUserId(list.get(0));
        }else {
            //2.2 如果不在购物车,先判断是菜品，还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //2.2.1 插入的是菜品
                //先查询对应菜品，准备待插入数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                //2.2.2 插入的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //设置公共数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    public List<ShoppingCart> list() {
        //获取当前微信用户userId
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId((Long) redisTemplate.opsForValue().get("user_4"));
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    public void clean() {
        shoppingCartMapper.clean((Long) redisTemplate.opsForValue().get("user_4"));
    }

}
